﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ResumeService;
using System.Web.Security;
using Avatar.MembershipService;
using AvatarHelper;
using Avatar.Model;

public partial class PersonWorkExperience : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PersonHelper.BasicInformationJobIntentionCheck();

        if (!IsPostBack)
        {
            ddl_Year2.Items.Add(new ListItem("年", "0"));
            for (int _i = 2011; _i > 1940; _i--)
            {
                ddl_Year1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Year2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }

            ddl_Month2.Items.Add(new ListItem("月", "0"));

            for (int _i = 1; _i < 13; _i++)
            {
                ddl_Month1.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
                ddl_Month2.Items.Add(new ListItem(_i.ToString(), _i.ToString()));
            }
            if (LoginManager.GetCurrentUser() != null)
            {
                var _worke = WorkExperienceManager.GetWorkExperience(LoginManager.GetCurrentUser().UserName);
                ListView1.DataSource = _worke;
                ListView1.DataBind();
            }
            using (var _adc = DBHelper.GetDataContext())
            {
                DropDownListHelper.Bind(ddl_Nature, _adc.CompanyNature.ToList(), "Nature", "ID");
                DropDownListHelper.Bind(ddl_CompanySize, _adc.CompanySize.ToList(), "Size", "ID");
                DropDownListHelper.Bind(ddl_Industry, _adc.Industry.ToList(), "name", "ID");
                DropDownListHelper.Bind(ddl_ExpectationMoney, _adc.ExpectationMoney.ToList(), "name", "ID");
                DropDownListHelper.Bind(ddl_Postion, _adc.Position.Where(p => p.FatherID == 0).ToList(), "name", "ID");
            }
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
        var _w = WorkExperienceManager.GetWorkExperience(_id);
        if (_w != null)
        {
            txt_ID.Value = _w.ID.ToString();
            ddl_Year1.SelectedValue = _w.BeginDate.Year.ToString();
            ddl_Month1.SelectedValue = _w.BeginDate.Month.ToString();
            if (_w.OverDate != DateTime.MinValue)
            {
                ddl_Year2.SelectedValue = _w.OverDate.Year.ToString();
                ddl_Month2.SelectedValue = _w.OverDate.Month.ToString();
            }
            CompanyNameTextBox.Text = _w.CompanyName;
            ddl_Industry.SelectedValue = _w.IndustryID.ToString();
            ddl_CompanySize.SelectedValue = _w.CompanySizeID.ToString();
            ddl_Nature.SelectedValue = _w.CompanyNatureID.ToString();
            DepartmentTextBox.Text = _w.Department;
            ddl_Postion.SelectedValue = _w.PositionID.ToString();
            DescriptionTextBox.Text = _w.Description;
        }
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            Button _btn = sender as Button;
            Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);

            WorkExperienceManager.Delete(_id);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_SaveNew_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void Save()
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(txt_ID.Value) ? Guid.Empty : new Guid(txt_ID.Value);
            DateTime _beginDate = Convert.ToDateTime(ddl_Year1.SelectedValue + "/" + ddl_Month1.SelectedValue);
            DateTime _overDate = DateTime.MinValue;
            if (ddl_Year2.SelectedValue != "0" && ddl_Month2.SelectedValue != "0")
            {
                _overDate = Convert.ToDateTime(ddl_Year2.SelectedValue + "/" + ddl_Month2.SelectedValue);
            }

            WorkExperienceManager.SaveWorkExperience(_id,
                Membership.GetUser().UserName,
                CompanyNameTextBox.Text,
                DepartmentTextBox.Text,
                Convert.ToInt32(ddl_Postion.SelectedValue),
                txt_PositionName.Text,
                Convert.ToInt32(ddl_CompanySize.SelectedValue),
                _beginDate,
                _overDate,
                DescriptionTextBox.Text,
                Convert.ToInt32(ddl_Industry.SelectedValue),
                Convert.ToInt32(ddl_Nature.SelectedValue)
                );
        }
        catch (Exception _e)
        {
            throw new Exception(_e.Message);
        }
    }
}
